var express = require('express');
var router = express.Router();
var cookie = require('cookie-parser');

/* GET home page. */
router.get('/', function(req, res, next) {
	try{
		req.VandB.find().toArray(function(err, items){
			if (err) {
				res.status(404).send('Bad Request');
			} else {
				var articles = new Array()
				var video = new Array()
				var anime = new Array()
				for (var i = 0; i < items.length; i++) {
					switch(items[i].type){
						case 'video':{
							video.push(items[i]);
							break;
						}
						case 'anime':{
							anime.push(items[i]);
							break;
						}
						case 'article':{
							articles.push(items[i]);
							break;
						}
					}
				}
				res.render('index',{
					'title':'welcome to star',
					'user':req.infos,
					'articles':articles,
					'video':video,
				});
			}
		})
	} catch(e){
		res.status(404).render('404',{})
	}
});

module.exports = router;
